package com.hl;

import com.hl.entity.Teacher;
import com.hl.mapper.TeacherMapper;
import com.hl.util.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * 常规单表CRUD测试
 * */
public class Test1CURD {
    public static void main(String[] args) throws IOException {
        // 1.读取mybatis-config.xml文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        // 2.创建sqlSessionFctoryBuild工厂构建器
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 3.构建一个sqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        // 4.创建一个会话 获取一个数据库连接
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5.获取mapper
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        // 引用了sql代码块的方法
        Teacher teacher = mapper.selectTeacherById(1);
        System.out.println(teacher);

/*        List<Teacher> teacherList = mapper.selectAllTeachers();
        for (Teacher teacher1 : teacherList) {
            System.out.println(teacher1);
        }*/

/*        Teacher teacher2 = new Teacher();
        teacher2.setCollegeId(2);
        teacher2.setTname("龙傲天");
        int row = mapper.addTeacher(teacher2);
        System.out.println(row > 0 ? "增添成功" : "增添失败");*/

/*        Teacher teacher3 = new Teacher();
        teacher3.setCollegeId(1);
        teacher3.setTname("萧炎");
        teacher3.setTid(7);
        int row2 = mapper.updateTeacherById(teacher3);
        System.out.println(row2 > 0 ? "修改成功" : "修改失败");*/

/*        int row3 = mapper.deleteTeacherById(7);
        System.out.println(row3 > 0 ? "成功删除" + row3 + "条数据" : "成功删除" + row3 + "条数据");*/

/*        List ids = new ArrayList<>();
        ids.add(9);
        ids.add(8);
        int row4 = mapper.deleteTeacherByIds(ids);
        System.out.println(row4 > 0 ? "成功删除" + row4 + "条数据" : "成功删除" + row4 + "条数据");*/

        // 进行增删该操作后需要提交才能生效
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }
}
